package com.datastructure.test.findkthtotail;

public class FindKthToTail {

    public static void main(String[] args) {

    }

    public ListNode FindKthToTail (ListNode pHead, int k) {
        // write code here
        if(pHead==null){
            return pHead;
        }
        int length = 0;
        ListNode temp = pHead;
        while (pHead!=null){
            length++;
            pHead=pHead.next;
        }
        if(k>length){
            return null;
        }
        int time = length-k;
        while (time>0){
            temp=temp.next;
            time--;
        }
        return temp;
    }
}

class ListNode {
   int val;
   ListNode next = null;
   public ListNode(int val) {
     this.val = val;
  }
}
